package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;
    //根据手机号查询会员信息
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //新增会员
    public void add(Member member) {
        //如果用户设置了密码，需要对密码进行md5加密
        String password = member.getPassword();
        if(password != null){
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    //根据月份查询会员数量
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> memberCounts = new ArrayList<>();
        if(months != null && months.size() > 0){
            for (String month : months) {//2019.01
                String endTime = month + ".31";
                Integer memberCount = memberDao.findMemberCountBeforeDate(endTime);
                memberCounts.add(memberCount);
            }
        }
        return memberCounts;
    }

    //会员性别占比
    @Override
    public Map<String, Object> findMemberSexAndCount() {
        Map<String, Object> result = new HashMap<>();
        //从数据库中查询所有性别及其总数
        List<Map<String, Object>> sexCount = memberDao.findMemberSexAndCount();//map里面包含{"sexTypes: "..."} {"sexCount":"..."}
        List<String> sexTypes = new ArrayList<>();
        for (Map<String, Object> map : sexCount) {
            String name = (String) map.get("name");
            sexTypes.add(name);
        }
        //添加性别类型到map中
        result.put("sexTypes", sexTypes);
        //添加性别类型及性别数量到map中
        result.put("sexCount", sexCount);
        return result;
    }

    //会员年龄段占比
    @Override
    public Map<String, Object> findMemberAgeAndCount() {
        Map<String, Object> result = new HashMap<>();
        //从数据库中查询所有性别及其总数
        List<Map<String, Object>> count = memberDao.findMemberAgeAndCount();//map里面包含{"ageTypes: "..."} {"ageCount":"..."}
        List<Map<String, Object>> ageCount = new ArrayList<>();
        List<String> ageTypes = new ArrayList<>();

        String ageTypesA = count.get(0).get("ageTypesA")+"";
        String ageTypesB = count.get(0).get("ageTypesB")+"";
        String ageTypesC = count.get(0).get("ageTypesC")+"";
        String ageTypesD = count.get(0).get("ageTypesD")+"";
        Map map = new HashMap();
        map.put("name","0-18");
        map.put("value",ageTypesA);
        ageCount.add(map);

        map = new HashMap();
        map.put("name","18-30");
        map.put("value",ageTypesB);
        ageCount.add(map);

        map = new HashMap();
        map.put("name","30-45");
        map.put("value",ageTypesC);
        ageCount.add(map);

        map = new HashMap();
        map.put("name","45以上");
        map.put("value",ageTypesD);
        ageCount.add(map);

        ageTypes.add("0-18");
        ageTypes.add("18-30");
        ageTypes.add("30-45");
        ageTypes.add("45以上");

        //添加性别类型到map中
        result.put("ageTypes", ageTypes);
        //添加性别类型及性别数量到map中
        result.put("ageCount", ageCount);
        return result;
    }

    //获取会员数量(按指定月份)
    @Override
    public Integer findMemberCountByMonth(String startTime, String endTime) {
        Map map = new HashMap();
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        Integer countMember = memberDao.findMemberCountByMonth(map);
        return countMember;
    }
}
